package com.thinkmore.business.service;

import com.thinkmore.business.bean.User;
import com.thinkmore.business.dao.UserTestDao;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Resource
    private UserTestDao userTestDao;

    public User getById(String id) {
        return userTestDao.getById(id);
    }

    public List<User> getByArrayIds(String[] ids) {
        return userTestDao.getByArrayIds(ids);
    }

    public List<User> getByListIds(List<String> ids) {
        return userTestDao.getByListIds(ids);
    }

    public List<User> getByListObj(List<User> users) {
        return userTestDao.getByListObj(users);
    }

    public User getByIdWithParam(String id) {
        return userTestDao.getByIdWithParam(id);
    }

    public User getByMap1(Map<String, Object> map) {
        return userTestDao.getByMap1(map);
    }

    public User getByMap2(Map<String, Object> map) {
        return userTestDao.getByMap2(map);
    }

    public User getByMap3(Map<String, Object> map) {
        return userTestDao.getByMap3(map);
    }

    public void saveUser(User user) {
        userTestDao.saveUser(user);
    }

    public int updateByUser(User user) {
        return userTestDao.updateByUser(user);
    }

    public User getWithIfAndWhere(User user) {
        return userTestDao.getWithIfAndWhere(user);
    }

    public User getWithTrim(User user) {
        return userTestDao.getWithTrim(user);
    }

    public int updateByUserWithTrim(User user) {
        return userTestDao.updateByUserWithTrim(user);
    }

    public int insertBatch(List<User> users) {
        return userTestDao.insertBatch(users);
    }

    public int delByIds(List<String> ids) {
        return userTestDao.delByIds(ids);
    }

    public int insertBatch2(List<User> users) {
        return userTestDao.insertBatch2(users);
    }

    @Autowired
    private SqlSessionFactoryBean sqlSessionFactory;

    public int insertBatch3(List<User> users) {
        SqlSession sqlSession = null;
        try {
            sqlSession = sqlSessionFactory.getObject().openSession(ExecutorType.BATCH);
            UserTestDao testDao = sqlSession.getMapper(UserTestDao.class);
            int result = 0;
            for (User user : users) {
                result += testDao.saveUser(user);
            }
            sqlSession.commit();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
        return 0;
    }

    public int insertBatch4(List<User> users) {
        Connection connection = null;
        int result = 0;
        try {
            connection = sqlSessionFactory.getObject().getConfiguration().getEnvironment().getDataSource().getConnection();
            String sql = "insert into user(id,user_name,password,ctime) values(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            for (int i = 0; i < users.size(); i++) {
                User user = users.get(i);
                ps.setString(1, user.getId());
                ps.setString(2, user.getUserName());
                ps.setString(3, user.getPassword());
                ps.setObject(4, user.getCtime());
                ps.addBatch();
                if ((i + 1) % 200 == 0 || i == users.size() - 1) {
                    ps.executeBatch();
                    ps.clearBatch();
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return 0;
    }

    public int truncateUserTable() {
        return userTestDao.truncateUserTable();
    }

    public User getUserRoleById(String id) {
        return userTestDao.getUserRoleById(id);
    }

    public User getUserRole2ById(String id) {
        return userTestDao.getUserRole2ById(id);
    }

    /*public Page<User> getUsersByPage(int roleId, int pageNo, int pageSize) {
        //最后一个参数为true表示，计算count并一起返回
        // 页码的方式，pageNo的值会替换为:(pageNo-1)*pageSize
        Page<User> page = PageHelper.startPage(pageNo, pageSize, true);
        //offset的方式，pageNo需要自己事先计算好
        // Page<User> page = PageHelper.offsetPage(pageNo, pageSize, true);
        userTestDao.getUsersByPage(roleId);

        //以上写法也可以直接简写为：
        // PageHelper.startPage(pageNo, pageSize, true).doSelectPage(() -> userTestDao.getUsersByPage(roleId));
        return page;
    }*/

    public int getTotalCount() {
        return userTestDao.getTotalCount();
    }

    public Map<String, Object> getUser(String id) {
        return userTestDao.getUser(id);
    }

    public List<Map> getUsers(int roleId) {
        return userTestDao.getUsers(roleId);
    }
}
